package dk.dashboardFP.panels.displayPanel {
	import dk.dashboardFP.info.DashboardFPInfo;
	import dk.dashboardFP.core.DashboardFPCore;
	import dk.dashboardFP.dfpLogger.DFPLogger;
	import dk.dashboardFP.elements.button.ButtonElement;
	import dk.dashboardFP.elements.button.ButtonElementEvent;
	import dk.dashboardFP.modelDataHandler.properties.PropertyEvent;
	import dk.dashboardFP.panels.displayPanel.core.DisplayEditorCore;
	import dk.dashboardFP.panels.panelElements.AbstractDashboardFPPanel;
	import dk.dashboardFP.utils.keyListener.KeyListener;

	/**
	 * @author josef
	 */
	public class DisplayPanel extends AbstractDashboardFPPanel {

		private var _oDisplayPanelControl : DisplayPanelControlSubPanel;
		private var _oDisplayEditorControl : DisplayEditorControl;
		private var _oDisplayOverlayHandler : DisplayOverlayHandler;
		private var _oDisplayEditorCore : DisplayEditorCore;
		private var _oDragController : DragController;
		private var _oGlobalViewControl : GlobalViewControl;

		public function DisplayPanel(doDashbordFPCore : DashboardFPCore) {
			super(doDashbordFPCore, "DisplayChain Editor");
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		override protected function setup() : void {
			super.setup();
			
			//:::::::
			// core 
			_oDisplayEditorCore = new DisplayEditorCore(this);
			
			//:::::::
			// display overlay handler 
			_oDisplayOverlayHandler = new DisplayOverlayHandler(this);
			
			//:::::::
			// global view controller
			_oGlobalViewControl = new GlobalViewControl(this); 
			
			//:::::::
			// drag controller 
			_oDragController = new DragController(this);
			
			//:::::::
			// editor control 
			_oDisplayEditorControl = new DisplayEditorControl(this);
			if (DisplayPanelControlModel(_oDisplayPanelControl.oSubPanelModel).oDisplayEditorEnabledProp.bValue) {
				_oDisplayEditorControl.enable();
			}
			
			//:::::::
			// register controls 
			_oDisplayPanelControl.registerControls();
			
			//:::::::
			// register fro bubbled events 
			_doPanelUI.addEventListener(ButtonElementEvent.CLICKED, onButtonElementClick);
			
			//:::::::
			// toggle key setup
			KeyListener.getInstance().addKeyDown(69, toggleEditorState) ;
		}

		private function toggleEditorState() : void {
			if (DisplayPanelControlModel(_oDisplayPanelControl.oSubPanelModel).oDisplayEditorEnabledProp.bValue) {
				DisplayPanelControlModel(_oDisplayPanelControl.oSubPanelModel).oDisplayEditorEnabledProp.set_bValue(false);
			} else {
				DisplayPanelControlModel(_oDisplayPanelControl.oSubPanelModel).oDisplayEditorEnabledProp.set_bValue(true);
			}
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// info
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		
		override protected function getInfoData() : String {
			return DashboardFPInfo.displayPanelInfo
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// button element click handler
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function onButtonElementClick(event : ButtonElementEvent) : void {
			switch((ButtonElement(event.target).oID.toString())) {
				case "inspect_dc":
					DFPLogger.inspectDisplayChain("Full DisplayChain", _oDashboardFB.stage, -1, "", true, 0);
					break;
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// subpanels
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		override protected function createPanelElements() : void {
			
			//:::::::
			// control 
			_oDisplayPanelControl = new DisplayPanelControlSubPanel(this, "Editor Controls");
			addSubPanel(_oDisplayPanelControl);
			
			DisplayPanelControlModel(_oDisplayPanelControl.oSubPanelModel).oDisplayEditorEnabledProp.addEventListener(PropertyEvent.VALUE_CHANGE, onEnabledToggled, false, 0, true);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// enable disable
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function onEnabledToggled(event : PropertyEvent = null) : void {
			if (DisplayPanelControlModel(_oDisplayPanelControl.oSubPanelModel).oDisplayEditorEnabledProp.bValue) {
				_oDisplayEditorControl.enable();
			} else {
				_oDisplayEditorControl.disable();
			}
		}

		public function get oDisplayOverlayHandler() : DisplayOverlayHandler {
			return _oDisplayOverlayHandler;
		}

		public function get oDisplayEditorCore() : DisplayEditorCore {
			return _oDisplayEditorCore;
		}

		public function get oDisplayEditorControl() : DisplayEditorControl {
			return _oDisplayEditorControl;
		}

		public function get oDragController() : DragController {
			return _oDragController;
		}

		public function get oGlobalViewControl() : GlobalViewControl {
			return _oGlobalViewControl;
		}
	}
}
